<?php
declare (strict_types=1);

namespace app\exam\logic\admin;

use app\admin\logic\system\Log;
use app\common\exception\LogicException;
use app\exam\model\admin\ModuleConfig as ModuleConfigModel;

/**
 * Class ModuleConfig
 *
 * @package app\exam\logic\admin
 */
class ModuleConfig extends ModuleConfigModel
{

    /**
     * 获取版块的配置数据
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/1/27
     *
     * @param int $moduleId
     * @return array|\think\Model
     */
    public function getModuleConfigByModuleId(int $moduleId)
    {
        return $this->where('module_id', $moduleId)->findOrEmpty();
    }

    /**
     * 保存配置
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/1/28
     *
     * @param       $moduleId
     * @param array $data
     */
    public function saveModuleConfig($moduleId, array $data)
    {
        $config = [
            'module_id' => $moduleId,
            'value'     => $data
        ];

        if (!$this->replace(true)->save($config)) {
            throw new LogicException('保存失败');
        }

        Log::write('更新了【' . $data['module_title'] . '】', 'module_config', 'update', true);
    }

}